package com.example.module_home.viewmodel

import androidx.lifecycle.viewModelScope
import com.example.lib_mvicore.viewmodel.BaseViewModel
import com.example.lib_net.entity.ResponseEntity
import com.example.module_home.intent.SimpleVideoIntent
import com.example.module_home.model.service.VideoService
import com.example.module_home.state.SimpleVideoState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/28
 */
class SimpleVideoViewModel:BaseViewModel<SimpleVideoIntent>() {
    val service: VideoService by lazy { VideoService() }

    private val _state = MutableStateFlow<SimpleVideoState>(SimpleVideoState.Init)
    val state: StateFlow<SimpleVideoState>
        get() = _state
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect {
                when (it) {
                    is SimpleVideoIntent.getSimpleVideo -> {
                        doSimpleVideos(it)
                    }
                    is SimpleVideoIntent.getRecommendSimpleVideo->{
                        doRecommendSimpleVideos(it)
                    }
                }
            }
        }
    }
    /**
     * 获取推荐的视频信息
     */
    private fun doRecommendSimpleVideos(it: SimpleVideoIntent.getRecommendSimpleVideo) {
        viewModelScope.launch(Dispatchers.IO) {
            flow {
                emit(service.getRecommendSimpleVideo(it.page,it.pagesize))
            }.catch { e->
                _state.value=SimpleVideoState.RecommendFailed
            }.filter {
                it != null
            }.collect{
                if (it.code==0){
                    if(it.data.isEmpty()){
                        _state.value = SimpleVideoState.Empty
                    }else{
                        _state.value=SimpleVideoState.RecommendSimpleVideosSuccess(it.data)
                    }

                }else{
                    _state.value=SimpleVideoState.RecommendFailed
                }
            }

        }
    }

    /**
     * 根据渠道id获取视频信息
     */
    private fun doSimpleVideos(it: SimpleVideoIntent.getSimpleVideo) {
        viewModelScope.launch(Dispatchers.IO) {
            flow{
                val data=service.getSimpleVideoByChannelId(it.channelId,it.page,it.pagesize)
                emit(data)
            }.catch { e->
                _state.value=SimpleVideoState.Failed
            }.filter {
                it != null
            }.collect {
                if (it.code==0){
                    if(it.data.isEmpty()){
                        _state.value = SimpleVideoState.Empty
                    }else{
                        _state.value=SimpleVideoState.SimpleVideosSuccess(it.data)
                    }
                }else {
                    _state.value=SimpleVideoState.Failed
                }
            }


        }
    }
}